package diona.search.yasearch

import java.net.{URL, URLEncoder}
import diona.search.{FoafProfile, ProfileInfo}
import xml.{NodeSeq, XML}

/**
 * User: anton
 * Date: 23.07.11 20:08
 */
object YaProfilesSearch
{

  private val searchUrl = "http://blogs.yandex.ru/search_profiles_atom.xml"

  @volatile var search: String => Option[ProfileInfo] = (profileLink) => {
    val profile = XML.load(new URL(searchUrl + "?" + "text=" + URLEncoder.encode(profileLink, "utf-8")))
    parseProfile(profile, profileLink)
  }

  def parseProfile(profile: NodeSeq, profileLink: String): Option[ProfileInfo] =
  {
    val entry = profile \ "entry"
    val content = entry \ "content"
    val rdf = content \ "RDF"
    rdf.headOption.map(FoafProfile(_))
      .map(foaf =>
      new ProfileInfo(
        profileLinks = List(profileLink),
        weblogs = foaf.weblogs.toList,
        name = foaf.name,
        city = foaf.city,
        country = foaf.country,
        region = foaf.region
      )
    )
  }
}